Skip to content

fix(test): add test environment isolation for worktree and RTK tests#3467

Merged
jeremymcs merged 1 commit intogsd-build:mainfrom
OfficialDelta:fix/test-environment-isolation
Apr 17, 2026
Merged

fix(test): add test environment isolation for worktree and RTK tests#3467
jeremymcs merged 1 commit intogsd-build:mainfrom
OfficialDelta:fix/test-environment-isolation

Conversation

@OfficialDelta
Copy link
Copy Markdown
Contributor

TL;DR

What: Fixes 13 test failures caused by test environment leaking into assertions.
Why: Tests read ~/.gsd/preferences.md (master vs main mismatch) and inherit GSD_RTK_DISABLED from the environment, causing failures on developer machines that pass in CI.
How: Added _resetServiceCache() for git service isolation, HOME override for preference isolation, and GSD_RTK_DISABLED cleanup in RTK tests.

What

  • worktree.ts: Added _resetServiceCache() to allow tests to clear cached GitServiceImpl
  • tests/integration/test-isolation.ts: Shared test isolation utilities
  • 5 worktree test files: Isolate from global ~/.gsd/preferences.md by resetting service cache and overriding HOME
  • 2 RTK test files: Clear GSD_RTK_DISABLED before running, restore after

Why

These 13 tests pass in CI (clean environment) but fail on developer machines where:

  • Global preferences set git.main_branch: master but test repos use main
  • GSD_RTK_DISABLED=1 is set, causing RTK snapshot tests to fail

Verified: all 13 failures are reproducible on current main and resolved by these changes.

How

Test isolation pattern: each affected test suite resets cached services in beforeEach and restores environment in afterEach. The _resetServiceCache() export is test-only (prefixed with underscore per convention).

Note: ~40 integration tests in tests/integration/ have the same git checkout master issue. This PR fixes unit tests only — integration test isolation is a follow-up.

- worktree.ts: Added _resetServiceCache() to allow tests to clear cached GitServiceImpl
- tests/integration/test-isolation.ts: Shared test isolation utilities
- 5 worktree test files: Isolate from global ~/.gsd/preferences.md by resetting service cache and overriding HOME
- 2 RTK test files: Clear GSD_RTK_DISABLED before running, restore after

These 13 tests pass in CI (clean environment) but fail on developer machines where:
- Global preferences set git.main_branch: master but test repos use main
- GSD_RTK_DISABLED=1 is set, causing RTK snapshot tests to fail
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 3, 2026

🟡 PR Risk Report — MEDIUM

Files changed 9
Systems affected 1
Overall risk 🟡 MEDIUM

Affected Systems

Risk System
🟡 medium Worktree
File Breakdown
Risk File Systems
🟡 src/resources/extensions/gsd/worktree.ts Worktree
src/resources/extensions/gsd/tests/integration/test-isolation.ts (unclassified)
src/resources/extensions/gsd/tests/stale-worktree-cwd.test.ts (unclassified)
src/resources/extensions/gsd/tests/stash-pop-gsd-conflict.test.ts (unclassified)
src/resources/extensions/gsd/tests/stash-queued-context-files.test.ts (unclassified)
src/resources/extensions/gsd/tests/worktree-integration.test.ts (unclassified)
src/resources/extensions/gsd/tests/worktree.test.ts (unclassified)
src/tests/rtk-session-stats.test.ts (unclassified)
src/tests/rtk.test.ts (unclassified)

@github-actions github-actions Bot added bug Something isn't working test labels Apr 3, 2026
@jeremymcs
Copy link
Copy Markdown
Collaborator

Hey @OfficialDelta — heads up that this PR currently has failing CI checks (windows-portability). The branch will need a fix before it can merge. Let us know if you need help diagnosing the failure.

🤖 Automated PR audit — 2026-04-04

@OfficialDelta
Copy link
Copy Markdown
Contributor Author

@jeremymcs I checked the windows-portability logs: the build, typecheck, and all test suites actually pass (223 pass, 0 fail in test:packages). The test:unit step shows 0 tests discovered, but that appears to be a pre-existing glob pattern issue on Windows (single-quoted globs in the npm script don't expand in PowerShell), not introduced by this PR.
If the check failure is a timeout rather than a test failure, is there a way to re-run it? Happy to investigate further if there's a specific error I'm missing.

@OfficialDelta
Copy link
Copy Markdown
Contributor Author

@jeremymcs I just wanted to follow up with my previous comment.

Copy link
Copy Markdown
Collaborator

@trek-e trek-e left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No linked issue — please open a GitHub issue for these test failures and add 'Closes #NNN' to the PR body.

The isolation approach is correct: resetting HOME + clearing path/service caches in before/after hooks prevents the developer's global preferences from bleeding into tests that use 'main' as the default branch. The GSD_RTK_DISABLED cleanup in the RTK test files is also correct.

One inconsistency: tests/integration/test-isolation.ts provides shared isolateFromGlobalPreferences() and restoreGlobalPreferences() utilities, but none of the test files in this PR actually import or use them — each file duplicates the isolation boilerplate inline. The shared utility has no consumers. Either use it (import and call it in the affected test files) or remove it.

The PR description accurately notes that ~40 integration tests in tests/integration/ have the same issue and are deferred to a follow-up. That is acceptable as long as a follow-up issue is opened.

@jeremymcs jeremymcs merged commit 624447f into gsd-build:main Apr 17, 2026
9 of 10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working test

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants